﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dpdw.Behaviour.CommandPattern
{
    /// <summary>
    /// 遥控器
    /// </summary>
    public class RemoteControl
    {
        private ICommand[] onCommands;
        private ICommand[] offCommands;

        private ICommand undoCommand;

        public RemoteControl()
        {
            onCommands = new ICommand[7];
            offCommands = new ICommand[7];

            NoCommand noCommand = new NoCommand();
            for (int i = 0; i < 7; i++)
            {
                onCommands[i] = noCommand;
                offCommands[i] = noCommand;
            }

            undoCommand = noCommand;
        }

        public void SetCommand(int slot, ICommand onCommand, ICommand offCommand)
        {
            onCommands[slot] = onCommand;
            offCommands[slot] = offCommand;
        }

        public void RemoveCommand(int slot)
        {
            NoCommand noCommand = new NoCommand();

            onCommands[slot] = noCommand;
            offCommands[slot] = noCommand;
        }

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="slot"></param>
        public void OnButtonWasPushed(int slot)
        {
            onCommands[slot].Execute();
            undoCommand = onCommands[slot];
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="slot"></param>
        public void OffButtonWasPushed(int slot)
        {
            offCommands[slot].Execute();
            undoCommand = offCommands[slot];
        }

        public void UndoButtonWasPushed()
        {
            undoCommand.Undo();
        }

        public override string ToString()
        {
            string namelist = "Remote Control\r\n";

            for (int i = 0; i < onCommands.Length; i++)
            {
                namelist = namelist + "[slot " + i.ToString() + "]"
                    + onCommands[i].Name + " " + offCommands[i].Name 
                    + "\r\n";
            }

            return namelist;
        }
    }
}
